<?php
include_once("survey_result_dao_base.php");

class survey_result_dao extends survey_result_dao_base
{
        function get_survey_result_list()
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$aos = array();
			$stmt = $dbh->query("SELECT * FROM survey_result ORDER BY survey_result_id DESC");
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$s = new survey_result($row["survey_result_id"],$row["survey_id"],$row["survey_result_content"],$row["mark_min"],$row["mark_max"],
$row["facebook_survey_result_content"]);
				$aos[] = $s;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aos;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function get_survey_result_list_by_survey_id($survey_id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$aos = array();
			$stmt = $dbh->query("SELECT * FROM survey_result sr WHERE sr.survey_id = ".$survey_id);
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$s = new survey_result($row["survey_result_id"],$row["survey_id"],$row["survey_result_content"],$row["mark_min"],$row["mark_max"],
$row["facebook_survey_result_content"]);
				$aos[] = $s;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aos;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	function get_survey_result_by_mark($survey_id,$mark){
	try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
				
			$stmt = $dbh->prepare("SELECT * FROM  survey_result s WHERE s.mark_min <= :mark1 and s.mark_max >= :mark2 and s.survey_id = :survey_id");
			$stmt->bindParam(':mark1', $mark, PDO::PARAM_INT);
			$stmt->bindParam(':mark2', $mark, PDO::PARAM_INT);
			$stmt->bindParam(':survey_id', $survey_id, PDO::PARAM_INT);
			$stmt->execute();
				
			$s = null;
			if($row = $stmt->fetch())
			{
				$s = new survey_result($row["survey_result_id"],$row["survey_id"],$row["survey_result_content"],$row["mark_min"],$row["mark_max"],$row["facebook_survey_result_content"]);
			}
				
			$dbh->commit();
			$dbh = null;
			return $s;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}
	}
}
?>
